package str;

public class L541 {
    public String reverseStr(String s, int k) {
        char[] charArray = s.toCharArray();
        for (int i=0;i<charArray.length;i+=2*k){
            // 2. 剩余字符小于 2k 但大于或等于 k 个，则反转前 k 个字符
            if (i + k <= charArray.length) {
                reverse(charArray, i, i + k -1);
                continue;
            }
            reverse(charArray, i, charArray.length - 1);// 剩余字符少于 k 个，则将剩余字符全部反转
        }
        return new String(charArray);

    }
    public void reverse(char[] ch, int i, int j) {
        for (; i < j; i++, j--) {
            char temp  = ch[i];
            ch[i] = ch[j];
            ch[j] = temp;
        }
    }

}
